/*
 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License. You
 *  may obtain a copy of the License at
 *
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  See the COPYRIGHT.txt file distributed with this work for information
 *  regarding copyright ownership.
 */
package org.mmtk.plan.colormark;

import org.mmtk.plan.*;
import org.mmtk.utility.Log;
import org.mmtk.utility.options.Options;
import org.mmtk.vm.VM;

import org.vmmagic.pragma.*;

/**
 * This class implements <i>per-collector thread</i> behavior and state for the
 * <i>MS</i> plan, which implements a full-heap mark-sweep collector.
 * <p>
 * 
 * Specifically, this class defines <i>MS</i> collection behavior (through
 * <code>trace</code> and the <code>collectionPhase</code> method).
 * <p>
 * 
 * @see CM for an overview of the mark-sweep algorithm.
 *      <p>
 * 
 * @see CM
 * @see CMMutator
 * @see StopTheWorldCollector
 * @see CollectorContext
 */
@Uninterruptible
public class CMCollector extends StopTheWorldCollector {

	/****************************************************************************
	 * Instance fields
	 */
	protected CMTraceLocal localTrace;
	protected CMTraceLocal currentTrace;
	protected CMHelper helper;
	private boolean firstClosure;
	
	public CMCollector() {
		localTrace= new CMTraceLocal(global().regularTrace);
	}
	/****************************************************************************
	 * Collection
	 */

	/**
	 * Perform a per-collector collection phase.
	 * 
	 * @param phaseId
	 *            The collection phase to perform
	 * @param primary
	 *            Perform any single-threaded activities using this thread.
	 */
	@Inline
	@Override
	public void collectionPhase(short phaseId, boolean primary) {
		if (Options.verbose.getValue() >= 3) {
			CM.acquireLogLock();
			Log.prependThreadId();
			Log.write("\tcollectionPhase entered with phaseId=");Log.write(phaseId);
			Log.write(", phaseName=");Log.write(Phase.getName(phaseId));
			Log.write(", primary=");Log.write(primary);
			Log.write(", helper=");
			if(helper == null) {
				Log.write("no");
			} else {
				Log.write("yes");
			}
			Log.write(", id=");Log.writeln(this.getId());
			Log.flush();
			CM.releaseLogLock();
		}
		
		if (phaseId == CM.PREPARE) {
			//nothing is done by the parent
			super.collectionPhase(phaseId, primary);
			//all the preparation is here
			prepareCollection(primary);
			this.firstClosure = true;
			return;
		}
		
		if (phaseId == CM.STACK_ROOTS) {
			super.collectionPhase(phaseId, primary);
			if(helper != null) {
				//make roots available to others
				localTrace.flush();
			}
			return;
		}
		
		if (phaseId == CM.ROOTS) {
			super.collectionPhase(phaseId, primary);
			if(helper != null) {
				//make roots available to others
				localTrace.flush();
			}
			return;
		}

		
		if (phaseId == CM.CLOSURE) {
			if(helper == null) {
				// regular threads trace from collected roots
				localTrace.completeTrace();
				if(this.firstClosure) {
					CM.reportTraceDone();
					this.firstClosure = false;
				}
			} else {
				// helper threads trace from random live objects
				if(this.firstClosure) {
					while(!CM.isTraceDone()) {
						helper.help();
					}
				}
			}
			return;
		}

		if (phaseId == CM.RELEASE) {
			localTrace.release();
			super.collectionPhase(phaseId, primary);
			return;
		}

		super.collectionPhase(phaseId, primary);
	}

	/****************************************************************************
	 * Miscellaneous
	 */

	/** @return The active global plan as an <code>MS</code> instance. */
	@Inline
	private static CM global() {
		return (CM) VM.activePlan.global();
	}

	/** @return The current trace instance. */
	@Override
	public final TraceLocal getCurrentTrace() {
		return currentTrace;
	}

	/**
	 * Decide which threads will be helpers and let helpers grab their resources
	 * 
	 * @param primary
	 */
	private void prepareCollection(boolean primary) {
		localTrace.prepare();
		currentTrace = localTrace;
		
		helper = CM.getHelper(primary);
		if(helper != null) {
			helper.prepare();
		} 		
	}
}
